import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split #继承划分训练集和测试机
from sklearn.neighbors import KNeighborsClassifier  #K近邻
import matplotlib.pyplot as plt
from collections import Counter     #用于统计分类正确的样本数
from sklearn.preprocessing import StandardScaler#导入用于归一化的包
from sklearn.model_selection import cross_val_score#k折
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler

AF_data_frame=pd.read_excel('RR_seg_AF.xls',sheet_name='Sheet1')
nonAF_data_frame=pd.read_excel('RR_seg_nonAF.xls',sheet_name='Sheet1')

#np格式
AF_input=AF_data_frame.to_numpy()
nonAF_input=nonAF_data_frame.to_numpy()


AF_label=np.ones(AF_input.shape[0])
nonAF_label=np.zeros(nonAF_input.shape[0])


AF_label=AF_label.flatten()
nonAF_label=nonAF_label.flatten()

X=np.concatenate([AF_input,nonAF_input])

from sklearn.decomposition import PCA
#pca=PCA(n_components='mle',svd_solver='full')
acc_list=[]
raw_y=np.concatenate([AF_label[0:],nonAF_label[0:]])
for i in range(1,31):
    pca=PCA(n_components=i,svd_solver='full')
    pca.fit(X)
    raw_x=pca.transform(X)

    #raw_x=X_PCA
    #raw_X=X
    ss=StandardScaler()
    raw_x=ss.fit_transform(raw_x)

    k=10
    m=400
    m_min=450
    m_max=451
    m_interval=2
    index=np.array(range(m_min,m_max+1,m_interval))
    for i in range(m_min,m_max+1,m_interval):
        model=KNeighborsClassifier(n_neighbors=i,weights='uniform')
        acc=cross_val_score(model,raw_x,raw_y,cv=k,scoring="accuracy")
        mean_acc=np.array(acc).mean()
        print(mean_acc)
        acc_list.append(mean_acc)
plt.plot(np.array(range(1,31)),acc_list)
plt.xlim(1,31)
plt.show()


